using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using JWalsh.XnaSamples.Framework;

namespace JWalsh.XnaSamples
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        KeyboardState _prevKeyboardState = Keyboard.GetState();
        //password svn pg5yV7bg4CN7
        Camera _camera;
        Model _mesh;
        private Model _player;
        FpsCamera camera;
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            IsMouseVisible = true;
            // Allow users to resize the window, and handle the Projection Matrix on Resize
            Window.Title = "Camera Test";
            Window.AllowUserResizing = true;
        }


        protected override void Initialize()
        {
            // Calculate the camera's current position.
            // Set the direction the camera points without rotation.

            //Vector3 cameraReference = new Vector3(0, 0, 1);
            //Matrix rotationMatrix = Matrix.CreateRotationY(avatarPosition);

            // Create a vector pointing the direction the camera is facing.

            //Vector3 transformedReference = Vector3.Transform(cameraReference, rotationMatrix);

            // Calculate the position the camera is looking at.

            //Vector3 cameraLookat = cameraPosition + transformedReference;

            // Set up the view matrix and projection matrix.

            //view = Matrix.CreateLookAt(cameraPosition, cameraLookat, new Vector3(0.0f, 1.0f, 0.0f));

            this.camera = new FpsCamera(graphics.GraphicsDevice.Viewport, new Vector3(0,0,500),new Vector3(0,10,0),0,0);

            //this._camera = new Camera(graphics.GraphicsDevice.Viewport);
            //this._camera.LookAt = new Vector3(0.0f, 0.0f, 5000f);
            this.IsMouseVisible = false;
            base.Initialize();
        }

        protected override void LoadContent()
        {

            _mesh = Content.Load<Model>("Brick");
            _player = Content.Load<Model>("player");
        }

        protected override void UnloadContent()
        {
            Content.Unload();
        }

        protected override void Update(GameTime gameTime)
        {
            //_camera.Update();
            camera.GetCurrentCamera();
            camera.Update(gameTime);
            KeyboardState keyboard = Keyboard.GetState();
            if (keyboard.IsKeyDown(Keys.Escape))
                Exit();
            _prevKeyboardState = keyboard;

        }

        protected override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

            DrawModel(_mesh, camera.Projection, camera.View, false);
            if (camera._cameraState == 2)
            {
                DrawModel(_player, camera.Projection, camera.View, true);
            }
            base.Draw(gameTime);

        }

        void DrawModel(Model model, Matrix proj, Matrix view, bool player)
        {
            Matrix[] transforms = new Matrix[model.Bones.Count];
            model.CopyAbsoluteBoneTransformsTo(transforms);
            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (BasicEffect be in mesh.Effects)
                {
                    be.EnableDefaultLighting();
                    be.Projection = proj;
                    be.View = view;
                    if (!player)
                    {
                       be.World = transforms[mesh.ParentBone.Index]*Matrix.CreateRotationY(0)*
                                   Matrix.CreateTranslation(0, 0, 0);
                    }
                    else
                    {
                       //be.World = Matrix.CreateRotationY(camera.AvatarYaw)*Matrix.CreateRotationZ(5) * Matrix.CreateTranslation(camera.PlayerPosition);
                        be.World = transforms[mesh.ParentBone.Index] * Matrix.CreateRotationY(MathHelper.ToRadians(180)) * Matrix.CreateTranslation(camera.PlayerPosition);
                    }
                    //be.Texture = texture;
                    //be.TextureEnabled = true;
                }
                mesh.Draw();
            }
        }
    }
}
